/**
 * 
 */
package com.jsync.system;

import java.io.File;
import java.io.FileFilter;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.Queue;
import java.util.regex.Pattern;

import org.apache.commons.io.filefilter.AgeFileFilter;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.RegexFileFilter;

import com.jsync.model.Resource;
import com.jsync.model.ResourceProfile;
import com.jsync.model.ResourceQueue;
import com.jsync.model.ResourceType;
import com.jsync.service.ResourceQueueManager;
import com.jsync.util.SingletonFactory;

/**
 * @author prabz
 *
 */
public class TimedResourceChangeThread implements Runnable {

	private ResourceProfile profile;
	private Calendar timestamp;
	private boolean stopFlag;
	private boolean runningFlag;
	
	public TimedResourceChangeThread(){
		stopFlag = false;
	}	
	
	/**
	 * @return the profile
	 */
	public ResourceProfile getProfile() {
		return profile;
	}

	/**
	 * @param profile the profile to set
	 */
	public void setProfile(ResourceProfile profile) {
		this.profile = profile;
	}

	/**
	 * @return the timestamp
	 */
	public Calendar getTimestamp() {
		return timestamp;
	}

	/**
	 * @param timestamp the timestamp to set
	 */
	public void setTimestamp(Calendar timestamp) {
		this.timestamp = timestamp;
	}

	/**
	 * @return the stopFlag
	 */
	public boolean isStopFlag() {
		return stopFlag;
	}

	/**
	 * @param stopFlag the stopFlag to set
	 */
	public void setStopFlag(boolean stopFlag) {
		this.stopFlag = stopFlag;
	}

	/**
	 * @return the runningFlag
	 */
	public boolean isRunningFlag() {
		return runningFlag;
	}

	/**
	 * @param runningFlag the runningFlag to set
	 */
	public void setRunningFlag(boolean runningFlag) {
		this.runningFlag = runningFlag;
	}

	@Override
	public void run() {
		runningFlag = true;
		// TODO Auto-generated method stub
		if(timestamp!=null && profile!=null){
			AgeFileFilter ageFilter = new AgeFileFilter(timestamp.getTime(), false);
			RegexFileFilter regexFilter = new RegexFileFilter(profile.getRegexFilter(), Pattern.CASE_INSENSITIVE);
			AndFileFilter andFilter = new AndFileFilter(ageFilter, regexFilter);			
			Queue<File> dirQueue = new LinkedList<File>();			
			File startDir = new File(profile.getSource());
			ResourceQueue resourceQueue = ((ResourceQueueManager)SingletonFactory.getInstance().get(ResourceQueueManager.class)).createResourceQueue(profile);			
			
			// Add the starting directory
			dirQueue.add(startDir);
			while(!dirQueue.isEmpty() && !isStopFlag()){
				File currentDir = dirQueue.poll();
				File[] files = currentDir.listFiles((FileFilter)ageFilter);
				for (File file : files) {
					// dont process the filtered file
					if(file.getName().toLowerCase().matches(profile.getRegexFilter()))
						continue;
					// Add sub directories to the directory queue
					if(file.isDirectory()){
						dirQueue.add(file);
					}else{	// Add the file to the ResourceQueue
						Resource resource = new Resource();
						resource.setProfile(profile);						
						resource.setName(file.getPath().substring(profile.getSource().replace('/', '\\').length()+1));
						resource.setChange(Resource.Operation.MODIFY);						
						resourceQueue.add(resource);
					}					
				}
			}			
		}
		System.out.println("Testing TimedResourceChange Thread exiting...");
		runningFlag = false;
	}

	public static void main(String[] args){
		ResourceProfile rp = new ResourceProfile();
		rp.setSource("F:/Work/S1/trunk");
		rp.setSourceType(ResourceType.WINDOWS);
		rp.setFilters("*.class;*.svn*;*.properties");

		Calendar timestamp = Calendar.getInstance();
		timestamp.add(Calendar.DATE, -1);
		TimedResourceChangeThread trc = new TimedResourceChangeThread();
		trc.setProfile(rp);		
		trc.setTimestamp(timestamp);
		(new Thread(trc)).start();
		
		try{
			do{
				Thread.sleep(1000);
			}while(trc.isRunningFlag());
		}catch(InterruptedException ex){
			ex.printStackTrace();
		}
				
		// Print out the changed files
		ResourceQueue resourceQueue = ((ResourceQueueManager)SingletonFactory.getInstance().get(ResourceQueueManager.class)).createResourceQueue(rp);
		while(!resourceQueue.isEmpty()){
			Resource resource = resourceQueue.remove();
			System.out.println("==> "+resource.getProfile().getSource()+File.pathSeparator+resource.getName());
		}
		
		System.out.println("Testing Main Thread exiting...");		
	}
}
